home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Mindy / Mindy 1.2 - portable sources / comp / src.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  41.9 KB  |  1,942 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: src.c,v 1.23 94/10/05 20:55:59 nkramer Exp $
  27. *
  28. * This file implements the various nodes in the parse tree.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include "../compat/std-c.h"
  33.  
  34. #include "mindycomp.h"
  35. #include "sym.h"
  36. #include "lexer.h"
  37. #include "literal.h"
  38. #include "src.h"
  39. #include "info.h"
  40. #include "lose.h"
  41.  
  42. struct local_methods {
  43.     struct method *head;
  44.     struct method **tail;
  45. };
  46.  
  47. struct binop_series {
  48.     struct binop *head;
  49.     struct binop **tail;
  50. };
  51.  
  52. struct arglist {
  53.     struct argument *head;
  54.     struct argument **tail;
  55. };
  56.  
  57. struct block_epilog {
  58.     struct exception_clause *inner;
  59.     struct body *cleanup;
  60.     struct exception_clause *outer;
  61. };
  62.  
  63. struct incomplete_condition_body {
  64.     struct constituent *constituents;
  65.     struct condition_body *rest;
  66. };
  67.  
  68. struct exception_clauses {
  69.     struct exception_clause *head;
  70.     struct exception_clause **tail;
  71. };
  72.  
  73. struct superclass_list {
  74.     struct superclass *head;
  75.     struct superclass **tail;
  76. };
  77.  
  78. struct for_header {
  79.     struct for_clause *clauses;
  80.     struct expr *until;
  81. };
  82.  
  83. struct gf_suffix {
  84.     struct return_type_list *rettypes;
  85.     struct plist *plist;
  86. };
  87.  
  88. struct to_part {
  89.     enum to_kind kind;
  90.     struct expr *expr;
  91. };
  92.  
  93. struct class_guts {
  94.     struct slot_spec *slots;
  95.     struct slot_spec **slots_tail;
  96.     struct initarg_spec *initargs;
  97.     struct initarg_spec **initargs_tail;
  98.     struct inherited_spec *inheriteds;
  99.     struct inherited_spec **inheriteds_tail;
  100. };
  101.  
  102. struct else_part {
  103.     int else_line;
  104.     struct body *alternate;
  105. };
  106.  
  107. struct body *make_body(void)
  108. {
  109.     struct body *res = malloc(sizeof(struct body));
  110.  
  111.     res->head = NULL;
  112.     res->tail = &res->head;
  113.  
  114.     return res;
  115. }
  116.  
  117. struct body
  118.     *add_constituent(struct body *body, struct constituent *constituent)
  119. {
  120.     if (constituent->kind == constituent_EXPR) {
  121.     struct expr *expr = ((struct expr_constituent *)constituent)->expr;
  122.     if (expr->kind == expr_BODY) {
  123.         struct body *insides = ((struct body_expr *)expr)->body;
  124.         struct constituent *c, **prev;
  125.  
  126.         *body->tail = insides->head;
  127.         /* Note: we can't use insides->tail because that will point */
  128.         /* inside the bindings established inside this block */
  129.         for (prev = body->tail; (c = *prev) != NULL; prev = &c->next)
  130.         ;
  131.         body->tail = prev;
  132.  
  133.         free(insides);
  134.         free(expr);
  135.         free(constituent);
  136.  
  137.         return body;
  138.     }
  139.     }
  140.         
  141.     *body->tail = constituent;
  142.  
  143.     switch (constituent->kind) {
  144.       case constituent_LET:
  145.       case constituent_LOCAL:
  146.       case constituent_HANDLER:
  147.     body->tail = ((struct binding_constituent *)constituent)->body->tail;
  148.     ((struct binding_constituent *)constituent)->body->tail = NULL;
  149.     break;
  150.       default:
  151.     body->tail = &constituent->next;
  152.     break;
  153.     }
  154.  
  155.     return body;
  156. }
  157.  
  158. struct body *make_expr_body(struct expr *expr)
  159. {
  160.     return add_constituent(make_body(), make_expr_constituent(expr));
  161. }
  162.  
  163. struct constituent *make_define_constant(int line, struct bindings *bindings)
  164. {
  165.     struct defconst_constituent *res
  166.     = malloc(sizeof(struct defconst_constituent));
  167.  
  168.     res->kind = constituent_DEFCONST;
  169.     res->next = NULL;
  170.     res->line = line;
  171.     res->bindings = bindings;
  172.     res->tlf = NULL;
  173.  
  174.     return (struct constituent *)res;
  175. }
  176.  
  177. struct constituent *make_define_method(flags_t flags, struct method *method)
  178. {
  179.     struct defmethod_constituent *res
  180.     = malloc(sizeof(struct defmethod_constituent));
  181.  
  182.     res->kind = constituent_DEFMETHOD;
  183.     res->next = NULL;
  184.     res->flags = flags;
  185.     res->method = method;
  186.     res->tlf = NULL;
  187.  
  188.     return (struct constituent *)res;
  189. }
  190.  
  191. struct constituent *make_define_variable(int line, struct bindings *bindings)
  192. {
  193.     struct defvar_constituent *res = malloc(sizeof(struct defvar_constituent));
  194.  
  195.     res->kind = constituent_DEFVAR;
  196.     res->next = NULL;
  197.     res->line = line;
  198.     res->bindings = bindings;
  199.     res->tlf = NULL;
  200.  
  201.     return (struct constituent *)res;
  202. }
  203.  
  204. struct constituent *make_expr_constituent(struct expr *expr)
  205. {
  206.     struct expr_constituent *res = malloc(sizeof(struct expr_constituent));
  207.  
  208.     res->kind = constituent_EXPR;
  209.     res->next = NULL;
  210.     res->expr = expr;
  211.  
  212.     return (struct constituent *)res;
  213. }
  214.  
  215. struct constituent *make_let(struct bindings *bindings)
  216. {
  217.     struct let_constituent *res    = malloc(sizeof(struct let_constituent));
  218.  
  219.     res->kind = constituent_LET;
  220.     res->next = NULL;
  221.     res->body = make_body();
  222.     res->offset = 0;
  223.     res->bindings = bindings;
  224.     res->required = 0;
  225.     res->lexenv = NULL;
  226.     res->inside = NULL;
  227.  
  228.     return (struct constituent *)res;
  229. }
  230.  
  231. struct constituent
  232.     *make_handler(struct expr *type, struct expr *func, struct plist *plist)
  233. {
  234.     struct handler_constituent *res
  235.     = malloc(sizeof(struct handler_constituent));
  236.  
  237.     res->kind = constituent_HANDLER;
  238.     res->next = NULL;
  239.     res->body = make_body();
  240.     res->type = type;
  241.     res->func = func;
  242.     res->plist = plist;
  243.  
  244.     return (struct constituent *)res;
  245. }
  246.  
  247. struct constituent *make_local_constituent(struct local_methods *methods)
  248. {
  249.     struct local_constituent *res
  250.     = malloc(sizeof(struct local_constituent));
  251.  
  252.     res->kind = constituent_LOCAL;
  253.     res->next = NULL;
  254.     res->body = make_body();
  255.     res->offset = 0;
  256.     res->methods = methods->head;
  257.     res->lexenv = NULL;
  258.  
  259.     free(methods);
  260.  
  261.     return (struct constituent *)res;
  262. }
  263.  
  264. struct constituent
  265.     *make_top_level_form(char *debug_name, struct constituent *c)
  266. {
  267.     struct tlf_constituent *res = malloc(sizeof(struct tlf_constituent));
  268.  
  269.     c->next = NULL;
  270.  
  271.     res->kind = constituent_TOPLEVELFORM;
  272.     res->next = NULL;
  273.     res->form
  274.     = make_top_level_method(debug_name, add_constituent(make_body(), c));
  275.  
  276.     return (struct constituent *)res;
  277. }
  278.  
  279. struct expr *make_varref(struct id *var)
  280. {
  281.     struct varref_expr *res = malloc(sizeof(struct varref_expr));
  282.  
  283.     res->kind = expr_VARREF;
  284.     res->analized = FALSE;
  285.     res->var = var;
  286.     res->home = NULL;
  287.     res->binding = NULL;
  288.     res->over = NULL;
  289.  
  290.     return (struct expr *)res;
  291. }
  292.  
  293. struct expr *make_varset(struct id *var, struct expr *expr)
  294. {
  295.     struct varset_expr *res = malloc(sizeof(struct varset_expr));
  296.  
  297.     res->kind = expr_VARSET;
  298.     res->analized = FALSE;
  299.     res->var = var;
  300.     res->home = NULL;
  301.     res->binding = NULL;
  302.     res->over = NULL;
  303.     res->value = expr;
  304.     res->type = NULL;
  305.  
  306.     return (struct expr *)res;
  307. }
  308.  
  309. struct id *id(struct symbol *symbol)
  310. {
  311.     struct id *res = malloc(sizeof(struct id));
  312.  
  313.     res->symbol = symbol;
  314.     res->internal = TRUE;
  315.     res->line = 0;
  316.  
  317.     return res;
  318. }
  319.  
  320. struct id *dup_id(struct id *id)
  321. {
  322.     struct id *res = malloc(sizeof(*res));
  323.  
  324.     memcpy(res, id, sizeof(*res));
  325.     res->line = 0;
  326.  
  327.     return res;
  328. }
  329.  
  330. struct id *make_id(struct token *token)
  331. {
  332.     char *ptr = (char *)token->chars;
  333.     struct id *res;
  334.  
  335.     if (*ptr == '\\')
  336.     ptr++;
  337.  
  338.     res = id(symbol(ptr));
  339.     res->internal = FALSE;
  340.     res->line = token->line;
  341.  
  342.     free(token);
  343.  
  344.     return res;
  345. }
  346.  
  347. struct bindings *make_bindings(struct param_list *params, struct expr *expr)
  348. {
  349.     struct bindings *res = malloc(sizeof(struct bindings));
  350.  
  351.     res->params = params;
  352.     res->expr = expr;
  353.  
  354.     return res;
  355. }
  356.  
  357. struct param_list *make_param_list(void)
  358. {
  359.     struct param_list *res = malloc(sizeof(struct param_list));
  360.  
  361.     res->required_params = NULL;
  362.     res->next_param = NULL;
  363.     res->rest_param = NULL;
  364.     res->allow_keys = FALSE;
  365.     res->all_keys = FALSE;
  366.     res->keyword_params = NULL;
  367.  
  368.     return res;
  369. }
  370.  
  371. struct param_list *push_param(struct param *param, struct param_list *list)
  372. {
  373.     param->next = list->required_params;
  374.     list->required_params = param;
  375.  
  376.     return list;
  377. }
  378.  
  379. struct param_list *set_next_param(struct param_list *list, struct id *var)
  380. {
  381.     list->next_param = var;
  382.  
  383.     return list;
  384. }
  385.  
  386. struct param_list *set_rest_param(struct param_list *list, struct id *var)
  387. {
  388.     list->rest_param = var;
  389.  
  390.     return list;
  391. }
  392.  
  393. struct param *make_param(struct id *id, struct expr *type)
  394. {
  395.     struct param *res = malloc(sizeof(struct param));
  396.  
  397.     res->id = id;
  398.     res->type = type;
  399.     res->type_temp = NULL;
  400.     res->next = NULL;
  401.  
  402.     return res;
  403. }
  404.  
  405. struct param_list
  406.     *push_keyword_param(struct keyword_param *param, struct param_list *list)
  407. {
  408.     param->next = list->keyword_params;
  409.     list->keyword_params = param;
  410.  
  411.     return list;
  412. }
  413.  
  414. struct param_list *allow_keywords(struct param_list *param_list)
  415. {
  416.     param_list->allow_keys = TRUE;
  417.  
  418.     return param_list;
  419. }
  420.  
  421. struct param_list *allow_all_keywords(struct param_list *param_list)
  422. {
  423.     param_list->allow_keys = TRUE;
  424.     param_list->all_keys = TRUE;
  425.  
  426.     return param_list;
  427. }
  428.  
  429. struct keyword_param
  430.     *make_keyword_param(struct token *key, struct id *sym, struct expr *type,
  431.             struct expr *def)
  432. {
  433.     struct keyword_param *res = malloc(sizeof(struct keyword_param));
  434.  
  435.     if (key) {
  436.     /* The keyword token has a trailing : */
  437.     key->chars[key->length-1] = '\0';
  438.     res->keyword = symbol((char *)key->chars);
  439.     free(key);
  440.     }
  441.     else
  442.     res->keyword = sym->symbol;
  443.  
  444.     res->id = sym;
  445.     res->type = type;
  446.     res->type_temp = NULL;
  447.     res->def = def;
  448.     res->next = NULL;
  449.  
  450.     return res;
  451. }
  452.  
  453. struct local_methods
  454.     *add_local_method(struct local_methods *methods, struct method *method)
  455. {
  456.     *methods->tail = method;
  457.     methods->tail = &method->next_local;
  458.  
  459.     return methods;
  460. }
  461.  
  462. struct local_methods *make_local_methods(void)
  463. {
  464.     struct local_methods *res = malloc(sizeof(struct local_methods));
  465.  
  466.     res->head = NULL;
  467.     res->tail = &res->head;
  468.  
  469.     return res;
  470. }
  471.  
  472. struct expr *make_literal_ref(struct literal *lit)
  473. {
  474.     struct literal_expr *res = malloc(sizeof(struct literal_expr));
  475.  
  476.     res->kind = expr_LITERAL;
  477.     res->analized = FALSE;
  478.     res->lit = lit;
  479.  
  480.     return (struct expr *)res;
  481. }
  482.  
  483. struct expr *make_binop_series_expr(struct expr *operand,
  484.                     struct binop_series *series)
  485. {
  486.     if (series->head) {
  487.     struct binop_series_expr *res
  488.         = malloc(sizeof(struct binop_series_expr));
  489.  
  490.     res->kind = expr_BINOP_SERIES;
  491.     res->analized = FALSE;
  492.     res->first_operand = operand;
  493.     res->first_binop = series->head;
  494.  
  495.     free(series);
  496.  
  497.     return (struct expr *)res;
  498.     }
  499.     else {
  500.     free(series);
  501.     return operand;
  502.     }
  503. }
  504.  
  505. struct binop_series *make_binop_series(void)
  506. {
  507.     struct binop_series *res = malloc(sizeof(struct binop_series));
  508.  
  509.     res->head = NULL;
  510.     res->tail = &res->head;
  511.  
  512.     return res;
  513. }
  514.  
  515. struct binop_series
  516.     *add_binop(struct binop_series *series, struct binop *op,
  517.            struct expr *operand)
  518. {
  519.     *series->tail = op;
  520.     series->tail = &op->next;
  521.     op->operand = operand;
  522.  
  523.     return series;
  524. }
  525.  
  526. struct binop *make_binop(struct id *id)
  527. {
  528.     struct binop *res = malloc(sizeof(struct binop));
  529.     struct binop_info *info = lookup_binop_info(id);
  530.  
  531.     res->op = id;
  532.     res->operand = NULL;
  533.     res->precedence = info->precedence;
  534.     res->left_assoc = info->left_assoc;
  535.     res->next = NULL;
  536.  
  537.     return res;
  538. }
  539.  
  540. static struct expr *make_unary_fn_call(struct expr *fn, struct expr *arg)
  541. {
  542.     struct arglist *args = make_argument_list();
  543.  
  544.     add_argument(args, make_argument(arg));
  545.  
  546.     return make_function_call(fn, args);
  547. }    
  548.  
  549. struct expr *make_negate(struct expr *expr)
  550. {
  551.     return make_unary_fn_call(make_varref(id(sym_Negative)), expr);
  552. }
  553.  
  554. static struct body *make_literal_body(struct literal *literal)
  555. {
  556.     return add_constituent(make_body(),
  557.                make_expr_constituent
  558.                    (make_literal_ref(literal)));
  559. }
  560.  
  561. struct expr *make_not(struct expr *expr)
  562. {
  563.     return make_if(expr, NULL,
  564.            make_else(0, make_literal_body(make_true_literal())));
  565. }
  566.  
  567. struct expr *make_singleton(struct expr *expr)
  568. {
  569.     return make_unary_fn_call(make_varref(id(sym_Singleton)), expr);
  570. }
  571.  
  572. struct expr *make_aref_or_element(struct expr *expr, struct arglist *args)
  573. {
  574.     struct argument *collection = make_argument(expr);
  575.  
  576.     collection->next = args->head;
  577.     args->head = collection;
  578.     /* This leaves args->tail wrong, but that doens't matter because */
  579.     /* because we just pass it directly to make_function_call */
  580.  
  581.     if (args->head->next != NULL && args->head->next->next == NULL)
  582.     return make_function_call(make_varref(id(sym_Element)), args);
  583.     else
  584.     return make_function_call(make_varref(id(sym_Aref)), args);
  585. }
  586.  
  587. struct expr *make_function_call(struct expr *expr, struct arglist *args)
  588. {
  589.     struct call_expr *res = malloc(sizeof(struct call_expr));
  590.  
  591.     res->kind = expr_CALL;
  592.     res->analized = FALSE;
  593.     res->func = expr;
  594.     if (expr->kind == expr_VARREF)
  595.     res->info = lookup_function_info(((struct varref_expr *)expr)->var,
  596.                      FALSE);
  597.     else
  598.     res->info = NULL;
  599.     res->args = args->head;
  600.  
  601.     free(args);
  602.  
  603.     return (struct expr *)res;
  604. }
  605.  
  606. struct expr *make_method_ref(struct method *method)
  607. {
  608.     struct method_expr *res = malloc(sizeof(struct method_expr));
  609.  
  610.     res->kind = expr_METHOD;
  611.     res->analized = FALSE;
  612.     res->method = method;
  613.  
  614.     return (struct expr *)res;
  615. }
  616.  
  617. struct expr *make_dot_operation(struct expr *arg, struct expr *func)
  618. {
  619.     struct dot_expr *res = malloc(sizeof(struct dot_expr));
  620.  
  621.     res->kind = expr_DOT;
  622.     res->analized = FALSE;
  623.     res->arg = arg;
  624.     res->func = func;
  625.  
  626.     return (struct expr *)res;
  627. }
  628.  
  629. struct arglist *make_argument_list(void)
  630. {
  631.     struct arglist *res = malloc(sizeof(struct arglist));
  632.  
  633.     res->head = NULL;
  634.     res->tail = &res->head;
  635.  
  636.     return res;
  637. }
  638.  
  639. struct arglist *add_argument(struct arglist *arglist, struct argument *arg)
  640. {
  641.     *arglist->tail = arg;
  642.     while (arg->next != NULL)
  643.     arg = arg->next;
  644.     arglist->tail = &arg->next;
  645.  
  646.     return arglist;
  647. }
  648.  
  649. struct argument *make_argument(struct expr *expr)
  650. {
  651.     struct argument *res = malloc(sizeof(struct argument));
  652.  
  653.     res->expr = expr;
  654.     res->next = NULL;
  655.  
  656.     return res;
  657. }
  658.  
  659. struct argument
  660.     *make_keyword_argument(struct token *keyword, struct expr *expr)
  661. {
  662.     struct argument *keyarg
  663.     = make_argument(make_literal_ref(parse_keyword_token(keyword)));
  664.  
  665.     keyarg->next = make_argument(expr);
  666.  
  667.     return keyarg;
  668. }
  669.  
  670. struct plist *make_property_list(void)
  671. {
  672.     struct plist *res = malloc(sizeof(struct plist));
  673.  
  674.     res->head = NULL;
  675.     res->tail = &res->head;
  676.  
  677.     return res;
  678. }
  679.  
  680. struct plist
  681.     *add_property(struct plist *plist, struct token *key, struct expr *expr)
  682. {
  683.     struct property *prop = malloc(sizeof(struct property));
  684.  
  685.     /* The keyword token has a trailing : */
  686.     key->chars[key->length-1] = '\0';
  687.  
  688.     prop->line = key->line;
  689.     prop->keyword = symbol((char *)key->chars);
  690.     prop->expr = expr;
  691.     prop->next = NULL;
  692.  
  693.     *plist->tail = prop;
  694.     plist->tail = &prop->next;
  695.  
  696.     free(key);
  697.  
  698.     return plist;
  699. }
  700.  
  701. struct return_type_list *make_return_type_list(boolean restp,
  702.                            struct expr *rest)
  703. {
  704.     struct return_type_list *res = malloc(sizeof(struct return_type_list));
  705.  
  706.     res->req_types = NULL;
  707.     res->req_types_tail = &res->req_types;
  708.     res->req_types_list = NULL;
  709.     res->restp = restp;
  710.     res->rest_type = rest;
  711.     res->rest_temp = NULL;
  712.     res->rest_temp_varref = NULL;
  713.  
  714.     return res;
  715. }
  716.  
  717. struct return_type_list *add_return_type(struct return_type_list *list,
  718.                      struct expr *type)
  719. {
  720.     struct return_type *rtype = malloc(sizeof(struct return_type));
  721.  
  722.     rtype->type = type;
  723.     rtype->temp = NULL;
  724.     rtype->next = NULL;
  725.     *list->req_types_tail = rtype;
  726.     list->req_types_tail = &rtype->next;
  727.  
  728.     return list;
  729. }
  730.  
  731. struct return_type_list
  732.     *set_return_type_rest_type(struct return_type_list *list,
  733.                    struct expr *type)
  734. {
  735.     list->restp = TRUE;
  736.     list->rest_type = type;
  737.     return list;
  738. }
  739.  
  740. struct literal *parse_true_token(struct token *token)
  741. {
  742.     struct literal *res = make_true_literal();
  743.     res->line = token->line;
  744.     free(token);
  745.     return res;
  746. }
  747.  
  748. struct literal *parse_false_token(struct token *token)
  749. {
  750.     struct literal *res = make_false_literal();
  751.     res->line = token->line;
  752.     free(token);
  753.     return res;
  754. }
  755.  
  756. struct literal *parse_unbound_token(struct token *token)
  757. {
  758.     struct literal *res = make_unbound_literal();
  759.     res->line = token->line;
  760.     free(token);
  761.     return res;
  762. }
  763.  
  764. static int escape_char(int c)
  765. {
  766.     switch (c) {
  767.       case 'a': return '\007';
  768.       case 'b': return '\b';
  769.       case 'e': return '\033';
  770.       case 'f': return '\f';
  771.       case 'n': return '\n';
  772.       case 'r': return '\r';
  773.       case 't': return '\t';
  774.       case '0': return '\0';
  775.       default: return c;
  776.     }
  777. }
  778.  
  779. struct literal *parse_string_token(struct token *token)
  780. {
  781.     struct string_literal *res;
  782.     int length = token->length - 2;
  783.     int i;
  784.     char *src, *dst;
  785.  
  786.     src = (char *)token->chars + 1;
  787.     for (i = length; i > 0; i--) {
  788.     if (*src++ == '\\') {
  789.         length--;
  790.         i--;
  791.         src++;
  792.     }
  793.     }
  794.  
  795.     res = malloc(sizeof(struct string_literal) 
  796.          + length + 1 - sizeof(res->chars));
  797.  
  798.     res->kind = literal_STRING;
  799.     res->next = NULL;
  800.     res->line = token->line;
  801.     res->length = length;
  802.  
  803.     src = (char *)token->chars + 1;
  804.     dst = (char *)res->chars;
  805.     for (i = length; i > 0; i--) {
  806.     int c = *src++;
  807.     if (c == '\\')
  808.         *dst++ = escape_char(*src++);
  809.     else
  810.         *dst++ = c;
  811.     }
  812.     *dst++ = '\0';
  813.  
  814.     free(token);
  815.  
  816.     return (struct literal *)res;
  817. }
  818.  
  819. struct literal
  820.     *concat_string_token(struct literal *old_literal, struct token *token)
  821. {
  822.     struct string_literal *old = (struct string_literal *)old_literal;
  823.     int old_length = old->length;
  824.     char *old_string = (char *)old->chars;
  825.     struct string_literal *res;
  826.     int length = token->length - 2;
  827.     int i;
  828.     char *src, *dst;
  829.  
  830.     res = malloc(sizeof(struct string_literal)
  831.          + old_length + length + 1 - sizeof(res->chars));
  832.  
  833.     res->kind = literal_STRING;
  834.     res->next = NULL;
  835.     res->line = old_literal->line;
  836.  
  837.     strncpy((char *)res->chars, old_string, old_length);
  838.     src = (char *)token->chars + 1;
  839.     dst = (char *)res->chars + old_length;
  840.     for (i = 0; i < length; i++) {
  841.     int c = *src++;
  842.     if (c == '\\') {
  843.         *dst++ = escape_char(*src++);
  844.         length--;
  845.     } else
  846.         *dst++ = c;
  847.     }
  848.     *dst++ = '\0';
  849.  
  850.     res->length = length + old_length;
  851.     free(token);
  852.  
  853.     return (struct literal *)res;
  854. }
  855.  
  856. struct literal *parse_character_token(struct token *token)
  857. {
  858.     int c = token->chars[1];
  859.     struct literal *res;
  860.  
  861.     if (c == '\\')
  862.     c = escape_char(token->chars[2]);
  863.  
  864.     res = make_character_literal(c);
  865.     res->line = token->line;
  866.  
  867.     free(token);
  868.  
  869.     return res;
  870. }
  871.  
  872. struct literal *parse_integer_token(struct token *token)
  873. {
  874.     long value;
  875.     int count, radix = 0;
  876.     boolean negative;
  877.     char *ptr, *remnant;
  878.     struct literal *res;
  879.  
  880.     value = 0;
  881.     count = token->length;
  882.     ptr = (char *)token->chars;
  883.     if (*ptr == '#') {
  884.     switch (ptr[1]) {
  885.       case 'x': radix = 16; break;
  886.       case 'o': radix = 8; break;
  887.       case 'b': radix = 2; break;
  888.     }
  889.     ptr += 2;
  890.     count -= 2;
  891.     negative = FALSE;
  892.     }
  893.     else {
  894.     radix = 10;
  895.     if (*ptr == '-') {
  896.         negative = TRUE;
  897.         count--;
  898.         ptr++;
  899.     }
  900.     else {
  901.         negative = FALSE;
  902.         if (*ptr == '+') {
  903.         count--;
  904.         ptr++;
  905.         }
  906.     }
  907.     }
  908.     if (radix == 0)
  909.     lose("No radix in integer literal?");
  910.  
  911.     value = strtoul(ptr, &remnant, radix);
  912.     if (negative)
  913.       value = -value;
  914.  
  915.     if (remnant == ptr)
  916.       lose("Integer literal did not convert: %s\n", token->chars);
  917.  
  918.     if (*remnant != 0)
  919.       lose("Integer literal did not convert completely: %s left %s\n", token->chars, remnant);
  920.     
  921.     res = make_integer_literal(value);
  922.     res->line = token->line;
  923.  
  924.     free(token);
  925.  
  926.     return res;
  927. }
  928.  
  929. struct literal *parse_float_token(struct token *token)
  930. {
  931.     unsigned char c, *ptr, *remnant;
  932.     enum literal_kind kind = literal_SINGLE_FLOAT;
  933.     struct literal *res = NULL;
  934.  
  935.     for (ptr = token->chars; (c = *ptr) != '\0'; ptr++) {
  936.     if (c == 'e' || c == 'E')
  937.         break;
  938.     if (c == 's' || c == 'S') {
  939.         *ptr = 'e';
  940.         break;
  941.     }
  942.     if (c == 'd' || c == 'D') {
  943.         *ptr = 'e';
  944.         kind = literal_DOUBLE_FLOAT;
  945.         break;
  946.     }
  947.     if (c == 'x' || c == 'X') {
  948.         *ptr = 'e';
  949.         kind = literal_EXTENDED_FLOAT;
  950.         break;
  951.     }
  952.     }
  953.  
  954.     switch (kind) {
  955.       case literal_SINGLE_FLOAT:
  956.     {
  957.         struct single_float_literal *r = malloc(sizeof(*r));
  958.         res = (struct literal *)r;
  959.         r->value = strtod((char *)token->chars, (char **)&remnant);
  960.         break;
  961.     }
  962.       case literal_DOUBLE_FLOAT:
  963.     {
  964.         struct double_float_literal *r = malloc(sizeof(*r));
  965.         res = (struct literal *)r;
  966.         r->value = strtod((char *)token->chars, (char **)&remnant);
  967.         break;
  968.     }
  969.       case literal_EXTENDED_FLOAT:
  970.     {
  971.         struct extended_float_literal *r = malloc(sizeof(*r));
  972.         res = (struct literal *)r;
  973.         r->value = strtod((char *)token->chars, (char **)&remnant);
  974.         break;
  975.     }
  976.       default:
  977.     lose("Strange float literal kind.\n");
  978.     break;
  979.     }
  980.  
  981.     if (remnant == token->chars)
  982.       lose("Float literal did not convert: %s\n", token->chars);
  983.  
  984.     if (*remnant != 0)
  985.       lose("Float literal did not completely convert: %s left %s\n", token->chars, remnant);
  986.  
  987.     /* Other possible errors would be indicated by errno == ERANGE:
  988.      * a result value of +/- HUGE_VAL returned indicates overflow,
  989.      * a result value of 0 returned indicates underflow. */
  990.  
  991.     res->kind = kind;
  992.     res->next = NULL;
  993.     res->line = token->line;
  994.  
  995.     free(token);
  996.  
  997.     return res;
  998. }
  999.  
  1000. struct literal *parse_symbol_token(struct token *token)
  1001. {
  1002.     char *ptr = (char *)token->chars;
  1003.     struct literal *res;
  1004.  
  1005.     /* We modify the token here, but we don't care 'cause we will be */
  1006.     /* freeing it shortly. */
  1007.  
  1008.     if (*ptr == '\\')
  1009.     /* They used the \op quoting convention. */
  1010.     ptr++;
  1011.  
  1012.     res = make_symbol_literal(symbol(ptr));
  1013.     res->line = token->line;
  1014.  
  1015.     free(token);
  1016.  
  1017.     return res;
  1018. }
  1019.  
  1020. struct literal *parse_keyword_token(struct token *token)
  1021. {
  1022.     char *ptr = (char *)token->chars;
  1023.     struct literal *res;
  1024.  
  1025.     /* We modify the token here, but we don't care 'cause we will be */
  1026.     /* freeing it shortly. */
  1027.  
  1028.     /* keyword tokens have a trailing : or " */
  1029.     ptr[token->length-1] = '\0';
  1030.  
  1031.     /* Sometimes they also have a leading #" */
  1032.     if (*ptr == '#')
  1033.     ptr += 2;
  1034.  
  1035.     res = make_symbol_literal(symbol(ptr));
  1036.     res->line = token->line;
  1037.  
  1038.     free(token);
  1039.  
  1040.     return res;
  1041. }
  1042.  
  1043. struct expr *make_body_expr(struct body *body)
  1044. {
  1045.     if (body->head && body->head->kind == constituent_EXPR
  1046.       && body->head->next == NULL) {
  1047.     struct expr *res = ((struct expr_constituent *)body->head)->expr;
  1048.     free(body->head);
  1049.     free(body);
  1050.     return res;
  1051.     }
  1052.     else {
  1053.     struct body_expr *res = malloc(sizeof(struct body_expr));
  1054.  
  1055.     res->kind = expr_BODY;
  1056.     res->analized = FALSE;
  1057.     res->body = body;
  1058.  
  1059.     return (struct expr *)res;
  1060.     }
  1061. }
  1062.  
  1063. struct expr *make_block(int line, struct id *exit, struct body *body,
  1064.             struct block_epilog *epilog)
  1065. {
  1066.     struct block_expr *res = malloc(sizeof(struct block_expr));
  1067.  
  1068.     res->kind = expr_BLOCK;
  1069.     res->analized = FALSE;
  1070.     res->line = line;
  1071.     res->exit_fun = exit;
  1072.     res->body = body;
  1073.     if (epilog) {
  1074.     res->inner = epilog->inner;
  1075.     res->cleanup = epilog->cleanup;
  1076.     res->outer = epilog->outer;
  1077.     free(epilog);
  1078.     }
  1079.     else {
  1080.     res->inner = NULL;
  1081.     res->cleanup = NULL;
  1082.     res->outer = NULL;
  1083.     }
  1084.  
  1085.     return (struct expr *)res;
  1086. }
  1087.  
  1088. struct expr *make_case(struct condition_body *body)
  1089. {
  1090.     struct case_expr *res = malloc(sizeof(struct case_expr));
  1091.  
  1092.     res->kind = expr_CASE;
  1093.     res->analized = FALSE;
  1094.     res->body = body;
  1095.  
  1096.     return (struct expr *)res;
  1097. }
  1098.  
  1099. struct expr *make_if(struct expr *cond, struct body *consequent,
  1100.              struct else_part *else_part)
  1101. {
  1102.     struct if_expr *res = malloc(sizeof(struct if_expr));
  1103.  
  1104.     res->kind = expr_IF;
  1105.     res->analized = FALSE;
  1106.     res->cond = cond;
  1107.     if (consequent)
  1108.     res->consequent = consequent;
  1109.     else
  1110.     res->consequent = make_literal_body(make_false_literal());
  1111.     if (else_part) {
  1112.     res->else_line = else_part->else_line;
  1113.     res->alternate = else_part->alternate;
  1114.     free(else_part);
  1115.     }
  1116.     else {
  1117.     res->else_line = 0;
  1118.     res->alternate = make_literal_body(make_false_literal());
  1119.     }
  1120.  
  1121.     return (struct expr *)res;
  1122. }
  1123.  
  1124. struct else_part *make_else(int else_line, struct body *alternate)
  1125. {
  1126.     struct else_part *res = malloc(sizeof(*res));
  1127.  
  1128.     res->else_line = else_line;
  1129.     res->alternate = alternate;
  1130.  
  1131.     return res;
  1132. }
  1133.  
  1134. struct expr *make_for(struct for_header *header, struct body *body,
  1135.                  struct body *finally)
  1136. {
  1137.     struct for_expr *res = malloc(sizeof(struct for_expr));
  1138.  
  1139.     res->kind = expr_FOR;
  1140.     res->analized = FALSE;
  1141.     res->clauses = header->clauses;
  1142.     res->until = header->until;
  1143.     res->body = body;
  1144.     res->finally = finally;
  1145.  
  1146.     free(header);
  1147.  
  1148.     return (struct expr *)res;
  1149. }
  1150.  
  1151. struct expr *make_select(struct expr *expr, struct expr *by,
  1152.                 struct condition_body *body)
  1153. {
  1154.     struct select_expr *res = malloc(sizeof(struct select_expr));
  1155.  
  1156.     res->kind = expr_SELECT;
  1157.     res->analized = FALSE;
  1158.     res->expr = expr;
  1159.     res->by = by;
  1160.     res->body = body;
  1161.  
  1162.     return (struct expr *)res;
  1163. }
  1164.  
  1165. struct expr *make_loop(struct body *body)
  1166. {
  1167.     struct loop_expr *res = malloc(sizeof(struct loop_expr));
  1168.  
  1169.     res->kind = expr_LOOP;
  1170.     res->analized = FALSE;
  1171.     res->body = body;
  1172.     res->position = 0;
  1173.  
  1174.     return (struct expr *)res;
  1175. }
  1176.  
  1177. struct expr *make_repeat(void)
  1178. {
  1179.     struct repeat_expr *res = malloc(sizeof(struct repeat_expr));
  1180.  
  1181.     res->kind = expr_REPEAT;
  1182.     res->analized = FALSE;
  1183.     res->loop = NULL;
  1184.  
  1185.     return (struct expr *)res;
  1186. }
  1187.  
  1188. struct block_epilog *make_block_epilog(struct exception_clauses *inner,
  1189.                        struct body *cleanup,
  1190.                        struct exception_clauses *outer)
  1191. {
  1192.     struct block_epilog *res = malloc(sizeof(struct block_epilog));
  1193.  
  1194.     if (inner) {
  1195.     res->inner = inner->head;
  1196.     free(inner);
  1197.     }
  1198.     else
  1199.     res->inner = NULL;
  1200.     res->cleanup = cleanup;
  1201.     if (outer) {
  1202.     res->outer = outer->head;
  1203.     free(outer);
  1204.     }
  1205.     else
  1206.     res->outer = NULL;
  1207.  
  1208.     return res;
  1209. }
  1210.  
  1211. struct for_header *make_for_header(struct expr *until)
  1212. {
  1213.     struct for_header *res = malloc(sizeof(struct for_header));
  1214.  
  1215.     res->clauses = NULL;
  1216.     res->until = until;
  1217.  
  1218.     return res;
  1219. }
  1220.  
  1221. struct for_header *push_for_clause(struct for_clause *clause,
  1222.                    struct for_header *header)
  1223. {
  1224.     clause->next = header->clauses;
  1225.     header->clauses = clause;
  1226.  
  1227.     return header;
  1228. }
  1229.  
  1230. struct exception_clauses *make_exception_clauses(void)
  1231. {
  1232.     struct exception_clauses *res = malloc(sizeof(struct exception_clauses));
  1233.  
  1234.     res->head = NULL;
  1235.     res->tail = &res->head;
  1236.  
  1237.     return res;
  1238. }
  1239.  
  1240. struct exception_clauses
  1241.     *add_exception_clause(struct exception_clauses *clauses,
  1242.               struct exception_clause *clause)
  1243. {
  1244.     *clauses->tail = clause;
  1245.     clauses->tail = &clause->next;
  1246.  
  1247.     return clauses;
  1248. }
  1249.  
  1250. struct exception_clause
  1251.     *make_exception_clause(struct expr *type, struct id *condition,
  1252.                struct plist *plist, struct body *body)
  1253. {
  1254.     struct exception_clause *res = malloc(sizeof(struct exception_clause));
  1255.  
  1256.     res->type = type;
  1257.     res->condition = condition;
  1258.     res->plist = plist;
  1259.     res->body = body;
  1260.     res->next = NULL;
  1261.  
  1262.     return res;
  1263. }
  1264.  
  1265. struct condition_body
  1266.     *push_condition_clause(struct condition_clause *clause,
  1267.                struct condition_body *cond_body)
  1268. {
  1269.     struct condition_body *res = malloc(sizeof(struct condition_body));
  1270.  
  1271.     res->clause = clause;
  1272.     res->next = cond_body;
  1273.  
  1274.     return res;
  1275. }
  1276.  
  1277. struct condition_clause
  1278.     *make_otherwise_condition_clause(struct body *body)
  1279. {
  1280.     struct condition_clause *res = malloc(sizeof(struct condition_clause));
  1281.  
  1282.     res->conditions = NULL;
  1283.     res->body = body;
  1284.  
  1285.     return res;
  1286. }
  1287.  
  1288. struct incomplete_condition_body
  1289.     *make_incomplete_condition_clauses(struct constituent *constituent,
  1290.                        struct condition_body *rest)
  1291. {
  1292.     struct incomplete_condition_body *res
  1293.     = malloc(sizeof(struct incomplete_condition_body));
  1294.  
  1295.     res->constituents = constituent;
  1296.     res->rest = rest;
  1297.  
  1298.     return res;
  1299. }
  1300.  
  1301. struct incomplete_condition_body
  1302.     *push_condition_constituent(struct constituent *constituent,
  1303.                 struct incomplete_condition_body *body)
  1304. {
  1305.     constituent->next = body->constituents;
  1306.     body->constituents = constituent;
  1307.  
  1308.     return body;
  1309. }
  1310.  
  1311. struct condition_body
  1312.     *complete_condition_clauses(struct condition_clause *clause,
  1313.                 struct incomplete_condition_body *body)
  1314. {
  1315.     struct constituent *constit, *next;
  1316.     struct condition_body *res;
  1317.  
  1318.     for (constit = body->constituents; constit != NULL; constit = next) {
  1319.     next = constit->next;
  1320.     constit->next = NULL;
  1321.     add_constituent(clause->body, constit);
  1322.     }
  1323.     res = push_condition_clause(clause, body->rest);
  1324.  
  1325.     free(body);
  1326.  
  1327.     return res;
  1328. }
  1329.  
  1330. struct condition_clause
  1331.     *make_condition_clause(struct constituent *constituent)
  1332. {
  1333.     struct condition_clause *res = malloc(sizeof(struct condition_clause));
  1334.  
  1335.     res->conditions = NULL;
  1336.     res->body = add_constituent(make_body(), constituent);
  1337.  
  1338.     return res;
  1339. }
  1340.  
  1341. struct condition_clause
  1342.     *push_condition(struct expr *expr, struct condition_clause *clause)
  1343. {
  1344.     struct condition *cond = malloc(sizeof(struct condition));
  1345.  
  1346.     cond->cond = expr;
  1347.     cond->next = clause->conditions;
  1348.     clause->conditions = cond;
  1349.  
  1350.     return clause;
  1351. }
  1352.  
  1353. struct for_clause
  1354.     *make_equal_then_for_clause(struct param_list *vars, struct expr *equal,
  1355.                 struct expr *then)
  1356. {
  1357.     struct equal_then_for_clause *res
  1358.     = malloc(sizeof(struct equal_then_for_clause));
  1359.  
  1360.     res->kind = for_EQUAL_THEN;
  1361.     res->next = NULL;
  1362.     res->vars = vars;
  1363.     res->equal = equal;
  1364.     res->then = then;
  1365.  
  1366.     return (struct for_clause *)res;
  1367. }
  1368.  
  1369. struct for_clause
  1370.     *make_in_for_clause(struct param *var, struct param *keyed_by,
  1371.             struct expr *collection)
  1372. {
  1373.     struct in_for_clause *res = malloc(sizeof(*res));
  1374.     struct param_list *vars = make_param_list();
  1375.  
  1376.     if (keyed_by)
  1377.     push_param(keyed_by, vars);
  1378.     push_param(var, vars);
  1379.  
  1380.     res->kind = for_IN;
  1381.     res->next = NULL;
  1382.     res->vars = vars;
  1383.     res->collection = collection;
  1384.  
  1385.     return (struct for_clause *)res;
  1386. }
  1387.  
  1388. struct for_clause
  1389.     *make_from_for_clause(struct param *var, struct expr *from,
  1390.               struct to_part *to, struct expr *by)
  1391. {
  1392.     struct from_for_clause *res
  1393.     = malloc(sizeof(struct from_for_clause));
  1394.  
  1395.     res->kind = for_FROM;
  1396.     res->next = NULL;
  1397.     res->vars = push_param(var, make_param_list());
  1398.     res->from = from;
  1399.     if (to) {
  1400.     res->to_kind = to->kind;
  1401.     res->to = to->expr;
  1402.     free(to);
  1403.     }
  1404.     else {
  1405.     res->to_kind = to_UNBOUNDED;
  1406.     res->to = NULL;
  1407.     }
  1408.     res->by = by;
  1409.  
  1410.     return (struct for_clause *)res;
  1411. }
  1412.  
  1413. struct to_part *make_to(struct expr *expr)
  1414. {
  1415.     struct to_part *res = malloc(sizeof(struct to_part));
  1416.  
  1417.     res->kind = to_TO;
  1418.     res->expr = expr;
  1419.  
  1420.     return res;
  1421. }
  1422.  
  1423. struct to_part *make_above(struct expr *expr)
  1424. {
  1425.     struct to_part *res = malloc(sizeof(struct to_part));
  1426.  
  1427.     res->kind = to_ABOVE;
  1428.     res->expr = expr;
  1429.  
  1430.     return res;
  1431. }
  1432.  
  1433. struct to_part *make_below(struct expr *expr)
  1434. {
  1435.     struct to_part *res = malloc(sizeof(struct to_part));
  1436.  
  1437.     res->kind = to_BELOW;
  1438.     res->expr = expr;
  1439.  
  1440.     return res;
  1441. }
  1442.  
  1443. struct constituent
  1444.     *make_class_definition(struct id *name, struct superclass_list *supers,
  1445.                struct class_guts *guts)
  1446. {
  1447.     struct defclass_constituent *res
  1448.     = malloc(sizeof(struct defclass_constituent));
  1449.  
  1450.     res->kind = constituent_DEFCLASS;
  1451.     res->next = NULL;
  1452.     res->flags = 0;
  1453.     res->name = name;
  1454.     res->supers = supers->head;
  1455.     free(supers);
  1456.     if (guts) {
  1457.     res->slots = guts->slots;
  1458.     res->initargs = guts->initargs;
  1459.     res->inheriteds = guts->inheriteds;
  1460.     free(guts);
  1461.     }
  1462.     else {
  1463.     res->slots = NULL;
  1464.     res->initargs = NULL;
  1465.     res->inheriteds = NULL;
  1466.     }
  1467.     res->tlf1 = NULL;
  1468.     res->tlf2 = NULL;
  1469.  
  1470.     return (struct constituent *)res;
  1471. }
  1472.  
  1473. struct constituent *set_class_flags(flags_t flags,
  1474.                     struct constituent *defclass)
  1475. {
  1476.     ((struct defclass_constituent *)defclass)->flags = flags;
  1477.     return defclass;
  1478. }
  1479.  
  1480. struct superclass_list *make_superclass_list(void)
  1481. {
  1482.     struct superclass_list *res = malloc(sizeof(struct superclass_list));
  1483.  
  1484.     res->head = NULL;
  1485.     res->tail = &res->head;
  1486.  
  1487.     return res;
  1488. }
  1489.  
  1490. struct superclass_list
  1491.     *add_superclass(struct superclass_list *list, struct expr *expr)
  1492. {
  1493.     struct superclass *sup = malloc(sizeof(struct superclass));
  1494.  
  1495.     sup->expr = expr;
  1496.     sup->next = NULL;
  1497.  
  1498.     *list->tail = sup;
  1499.     list->tail = &sup->next;
  1500.  
  1501.     return list;
  1502. }
  1503.  
  1504. struct class_guts *make_class_guts(void)
  1505. {
  1506.     struct class_guts *res = malloc(sizeof(*res));
  1507.  
  1508.     res->slots = NULL;
  1509.     res->slots_tail = &res->slots;
  1510.     res->initargs = NULL;
  1511.     res->initargs_tail = &res->initargs;
  1512.     res->inheriteds = NULL;
  1513.     res->inheriteds_tail = &res->inheriteds;
  1514.  
  1515.     return res;
  1516. }
  1517.  
  1518. struct slot_spec
  1519.     *make_slot_spec(int line, flags_t flags, enum slot_allocation alloc,
  1520.             struct id *name, struct expr *type, struct plist *plist)
  1521. {
  1522.     struct slot_spec *res = malloc(sizeof(struct slot_spec));
  1523.  
  1524.     res->line = line;
  1525.     res->flags = flags;
  1526.     res->alloc = alloc;
  1527.     res->name = name;
  1528.     res->type = type;
  1529.     res->plist = plist;
  1530.     res->getter = NULL;
  1531.     res->setter = NULL;
  1532.     res->next = NULL;
  1533.  
  1534.     return res;
  1535. }
  1536.  
  1537. struct class_guts *add_slot_spec(struct class_guts *guts,
  1538.                  struct slot_spec *spec)
  1539. {
  1540.     *guts->slots_tail = spec;
  1541.     guts->slots_tail = &spec->next;
  1542.  
  1543.     return guts;
  1544. }
  1545.  
  1546. struct initarg_spec
  1547.   *make_initarg_spec(boolean required, struct token *key, struct plist *plist)
  1548. {
  1549.     struct initarg_spec *res = malloc(sizeof(*res));
  1550.  
  1551.     /* The keyword token has a trailing : */
  1552.     key->chars[key->length-1] = '\0';
  1553.  
  1554.     res->required = required;
  1555.     res->keyword = symbol((char *)key->chars);
  1556.     res->plist = plist;
  1557.     res->next = NULL;
  1558.  
  1559.     return res;
  1560. }
  1561.  
  1562. struct class_guts *add_initarg_spec(struct class_guts *guts,
  1563.                     struct initarg_spec *spec)
  1564. {
  1565.     *guts->initargs_tail = spec;
  1566.     guts->initargs_tail = &spec->next;
  1567.  
  1568.     return guts;
  1569. }
  1570.  
  1571. struct inherited_spec *make_inherited_spec(struct id *name,
  1572.                        struct plist *plist)
  1573. {
  1574.     struct inherited_spec *res = malloc(sizeof(*res));
  1575.  
  1576.     res->name = name;
  1577.     res->plist = plist;
  1578.     res->next = NULL;
  1579.  
  1580.     return res;
  1581. }
  1582.  
  1583. struct class_guts *add_inherited_spec(struct class_guts *guts,
  1584.                       struct inherited_spec *spec)
  1585. {
  1586.     *guts->inheriteds_tail = spec;
  1587.     guts->inheriteds_tail = &spec->next;
  1588.  
  1589.     return guts;
  1590. }
  1591.  
  1592. struct constituent
  1593.     *make_define_generic(struct id *name, struct param_list *params,
  1594.              struct gf_suffix *suffix)
  1595. {
  1596.     struct defgeneric_constituent *res
  1597.     = malloc(sizeof(struct defgeneric_constituent));
  1598.  
  1599.     res->kind = constituent_DEFGENERIC;
  1600.     res->next = NULL;
  1601.     res->flags = 0;
  1602.     res->name = name;
  1603.     res->params = params;
  1604.     res->rettypes = suffix->rettypes;
  1605.     res->plist = suffix->plist;
  1606.     res->tlf = NULL;
  1607.  
  1608.     free(suffix);
  1609.  
  1610.     return (struct constituent *)res;
  1611. }
  1612.  
  1613. struct constituent *set_generic_flags(flags_t flags,
  1614.                     struct constituent *defgeneric)
  1615. {
  1616.     ((struct defgeneric_constituent *)defgeneric)->flags = flags;
  1617.     return defgeneric;
  1618. }
  1619.  
  1620. struct gf_suffix
  1621.     *make_gf_suffix(struct return_type_list *rettypes,
  1622.             struct plist *plist)
  1623. {
  1624.     struct gf_suffix *res = malloc(sizeof(struct gf_suffix));
  1625.  
  1626.     res->rettypes = rettypes;
  1627.     res->plist = plist;
  1628.  
  1629.     return res;
  1630. }
  1631.  
  1632. struct method *set_method_source(struct token *source, struct method *method)
  1633. {
  1634.     method->line = source->line;
  1635.  
  1636.     return method;
  1637. }
  1638.  
  1639. struct method *set_method_name(struct id *name, struct method *method)
  1640. {
  1641.     method->name = name;
  1642.     method->line = name->line;
  1643.     method->debug_name = make_symbol_literal(name->symbol);
  1644.  
  1645.     return method;
  1646. }
  1647.  
  1648. struct method
  1649.     *make_method_description(struct param_list *params,
  1650.                  struct return_type_list *rettypes,
  1651.                  struct body *body)
  1652. {
  1653.     struct method *res = malloc(sizeof(struct method));
  1654.  
  1655.     res->name = NULL;
  1656.     res->line = 0;
  1657.     res->debug_name = NULL;
  1658.     res->top_level = FALSE;
  1659.     res->component = NULL;
  1660.     res->params = params;
  1661.     res->specializers = NULL;
  1662.     res->rettypes = rettypes;
  1663.     res->body = body;
  1664.     res->next_local = NULL;
  1665.     res->nargs = 0;
  1666.     res->lexenv = NULL;
  1667.     res->frame_size = 0;
  1668.     res->closes_over = NULL;
  1669.     res->lexenv_size = 0;
  1670.     res->parent = NULL;
  1671.     res->kids = NULL;
  1672.     res->next = NULL;
  1673.  
  1674.     return res;
  1675. }
  1676.  
  1677. struct method *make_top_level_method(char *debug_name, struct body *body)
  1678. {
  1679.     struct method *res = make_method_description(make_param_list(),NULL,body);
  1680.  
  1681.     res->debug_name = make_string_literal(debug_name);
  1682.     res->top_level = TRUE;
  1683.     res->specializers=make_literal_ref(make_list_literal(make_literal_list()));
  1684.  
  1685.     return res;
  1686. }
  1687.  
  1688. struct constituent *make_error_constituent(void)
  1689. {
  1690.     struct constituent *res = malloc(sizeof(struct constituent));
  1691.  
  1692.     res->kind = constituent_ERROR;
  1693.     res->next = NULL;
  1694.  
  1695.     return res;
  1696. }
  1697.  
  1698. struct expr *make_error_expression(void)
  1699. {
  1700.     struct expr *res = malloc(sizeof(struct expr));
  1701.  
  1702.     res->kind = expr_ERROR;
  1703.     res->analized = FALSE;
  1704.  
  1705.     return res;
  1706. }
  1707.  
  1708. struct defnamespace_constituent
  1709.     *make_define_namespace(enum constituent_kind kind)
  1710. {
  1711.     struct defnamespace_constituent *res = malloc(sizeof(*res));
  1712.  
  1713.     res->kind = kind;
  1714.     res->next = NULL;
  1715.     res->name = NULL;
  1716.     res->use_clauses = NULL;
  1717.     res->use_tail = &res->use_clauses;
  1718.     res->exported_variables = make_variable_names();
  1719.     res->created_variables = make_variable_names();
  1720.     res->exported_literal = NULL;
  1721.     res->created_literal = NULL;
  1722.  
  1723.     return res;
  1724. }
  1725.  
  1726. struct defnamespace_constituent *make_define_module(void)
  1727. {
  1728.     return make_define_namespace(constituent_DEFMODULE);
  1729. }
  1730.  
  1731. struct defnamespace_constituent *make_define_library(void)
  1732. {
  1733.     return make_define_namespace(constituent_DEFLIBRARY);
  1734. }
  1735.  
  1736. struct defnamespace_constituent
  1737.     *set_namespace_name(struct defnamespace_constituent *namespace,
  1738.             struct token *name)
  1739. {
  1740.     namespace->name = parse_symbol_token(name);
  1741.  
  1742.     return namespace;
  1743. }
  1744.  
  1745. struct defnamespace_constituent
  1746.     *add_use_clause(struct defnamespace_constituent *namespace,
  1747.             struct use_clause *clause)
  1748. {
  1749.     *namespace->use_tail = clause;
  1750.     namespace->use_tail = &clause->next;
  1751.  
  1752.     return namespace;
  1753. }
  1754.  
  1755. struct defnamespace_constituent
  1756.     *add_exports(struct defnamespace_constituent *namespace,
  1757.          struct variable_names *vars)
  1758. {
  1759.     *namespace->exported_variables->tail = vars->head;
  1760.     namespace->exported_variables->tail = vars->tail;
  1761.     free(vars);
  1762.  
  1763.     return namespace;
  1764. }
  1765.  
  1766. struct defnamespace_constituent
  1767.     *add_creates(struct defnamespace_constituent *namespace,
  1768.          struct variable_names *vars)
  1769. {
  1770.     *namespace->created_variables->tail = vars->head;
  1771.     namespace->created_variables->tail = vars->tail;
  1772.     free(vars);
  1773.  
  1774.     return namespace;
  1775. }
  1776.  
  1777. struct use_clause
  1778.     *make_use_clause(struct token *symbol, struct use_options *options)
  1779. {
  1780.     struct use_clause *res = malloc(sizeof(*res));
  1781.  
  1782.     res->name = parse_symbol_token(symbol);
  1783.     res->options = options->head;
  1784.     res->next = NULL;
  1785.     res->import = NULL;
  1786.     res->exclude = NULL;
  1787.     res->prefix = NULL;
  1788.     res->rename = NULL;
  1789.     res->export = NULL;
  1790.  
  1791.     free(options);
  1792.  
  1793.     return res;
  1794. }
  1795.  
  1796. struct use_options *make_use_options(void)
  1797. {
  1798.     struct use_options *res = malloc(sizeof(*res));
  1799.  
  1800.     res->head = NULL;
  1801.     res->tail = &res->head;
  1802.  
  1803.     return res;
  1804. }
  1805.  
  1806. struct use_options
  1807.     *add_use_option(struct use_options *options, struct use_option *option)
  1808. {
  1809.     *options->tail = option;
  1810.     options->tail = &option->next;
  1811.  
  1812.     return options;
  1813. }
  1814.  
  1815. struct use_option *make_use_option(enum useopt_kind kind)
  1816. {
  1817.     struct use_option *res = malloc(sizeof(*res));
  1818.  
  1819.     res->kind = kind;
  1820.     res->next = NULL;
  1821.  
  1822.     return res;
  1823. }
  1824.  
  1825. struct use_option *make_prefix_option(struct token *token)
  1826. {
  1827.     struct prefix_option *res = malloc(sizeof(*res));
  1828.  
  1829.     res->kind = useopt_PREFIX;
  1830.     res->next = NULL;
  1831.     res->prefix = parse_string_token(token);
  1832.  
  1833.     return (struct use_option *) res;
  1834. }
  1835.  
  1836. struct variable_names *make_variable_names(void)
  1837. {
  1838.     struct variable_names *res = malloc(sizeof(*res));
  1839.  
  1840.     res->head = NULL;
  1841.     res->tail = &res->head;
  1842.  
  1843.     return res;
  1844. }
  1845.  
  1846. struct variable_names
  1847.     *add_variable_name(struct variable_names *names, struct token *token)
  1848. {
  1849.     struct variable_name *new = malloc(sizeof(*new));
  1850.     new->name = parse_symbol_token(token);
  1851.     new->next = NULL;
  1852.  
  1853.     *names->tail = new;
  1854.     names->tail = &new->next;
  1855.     
  1856.     return names;
  1857. }
  1858.  
  1859. struct renamings *make_renamings(void)
  1860. {
  1861.     struct renamings *res = malloc(sizeof(*res));
  1862.  
  1863.     res->head = NULL;
  1864.     res->tail = &res->head;
  1865.  
  1866.     return res;
  1867. }
  1868.  
  1869. struct renamings
  1870.     *add_renaming(struct renamings *names,
  1871.           struct token *from, struct token *to)
  1872. {
  1873.     struct renaming *new = malloc(sizeof(*new));
  1874.  
  1875.     new->from = parse_symbol_token(from);
  1876.     new->to = parse_symbol_token(to);
  1877.     new->next = NULL;
  1878.  
  1879.     *names->tail = new;
  1880.     names->tail = &new->next;
  1881.  
  1882.     return names;
  1883. }
  1884.  
  1885. struct import_option *make_import_option(void)
  1886. {
  1887.     struct import_option *res = malloc(sizeof(*res));
  1888.  
  1889.     res->kind = useopt_IMPORT;
  1890.     res->next = NULL;
  1891.     res->vars = make_variable_names();
  1892.     res->renames = make_renamings();
  1893.  
  1894.     return res;
  1895. }
  1896.  
  1897. struct import_option
  1898.     *add_import(struct import_option *opt,
  1899.         struct token *from, struct token *to)
  1900. {
  1901.     if (to)
  1902.     opt->renames = add_renaming(opt->renames, from, to);
  1903.     else
  1904.     opt->vars = add_variable_name(opt->vars, from);
  1905.  
  1906.     return opt;
  1907. }
  1908.  
  1909.  
  1910. struct use_option *make_exclude_option(struct variable_names *vars)
  1911. {
  1912.     struct exclude_option *res = malloc(sizeof(*res));
  1913.  
  1914.     res->kind = useopt_EXCLUDE;
  1915.     res->next = NULL;
  1916.     res->vars = vars;
  1917.  
  1918.     return (struct use_option *) res;
  1919. }
  1920.  
  1921. struct use_option *make_export_option(struct variable_names *vars)
  1922. {
  1923.     struct export_option *res = malloc(sizeof(*res));
  1924.  
  1925.     res->kind = useopt_EXPORT;
  1926.     res->next = NULL;
  1927.     res->vars = vars;
  1928.  
  1929.     return (struct use_option *) res;
  1930. }
  1931.  
  1932. struct use_option *make_rename_option(struct renamings *lst)
  1933. {
  1934.     struct rename_option *res = malloc(sizeof(*res));
  1935.  
  1936.     res->kind = useopt_RENAME;
  1937.     res->next = NULL;
  1938.     res->renames = lst;
  1939.  
  1940.     return (struct use_option *) res;
  1941. }
  1942.